package com.startdima.user.infrastructure.exception;

import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import com.startdima.user.infrastructure.dto.SddmResult;
import com.startdima.user.infrastructure.enums.LocaleEnum;

import lombok.extern.slf4j.Slf4j;

@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

 
    @ExceptionHandler(BusinessException.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public SddmResult handleException(BusinessException e) {
        log.error("业务异常: {}", e.getMessage());
        return new SddmResult(e.getCode(), e.getMessage());
    }
    
    @ExceptionHandler(DataIntegrityViolationException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ResponseBody
    public SddmResult handleException(DataIntegrityViolationException e) {
        log.error("异常: {}", e.getMessage());
        e.printStackTrace();
        return new SddmResult(500, e.getMessage());
    }
 
    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public SddmResult handleException(Exception e) {
        log.error("异常: {}", e.getMessage());
        e.printStackTrace();
        return new SddmResult(500, LocaleEnum.ERROR_INTERNAL_SERVER.name());
    }
    
}